<template>
  <div class="app-container">
    <SearchContent :queryParams="queryParams" @query="handleQuery" @reset="resetQuery">
      <el-col :span="6">
        <el-form-item label="车辆使用性质:" prop="vehicleUse">
          <el-select v-model="queryParams.vehicleUse" clearable>
            <template v-for="dict in dict.type.vehicle_use">
              <el-option :label="dict.label" :value="dict.value" :key="dict.value"></el-option>
            </template>
          </el-select>
        </el-form-item>
      </el-col>
      <el-col :span="6">
        <el-form-item label="标准公里(km):" prop="stdKilometers">
          <el-select v-model="queryParams.stdKilometers" clearable>
            <template v-for="dict in dict.type.kil_ome_tres">
              <el-option :label="dict.label" :value="dict.value" :key="dict.value"></el-option>
            </template>
          </el-select>
        </el-form-item>
      </el-col>
      <!-- <el-col :span="6">
        <el-form-item label="车系:" prop="seriesId">
          <el-select v-model="queryParams.seriesId" filterable clearable placeholder="请输入">
            <template v-for="dict in cityList">
              <el-option :label="dict.seriesName" :value="dict.id" :key="dict.id"></el-option>
            </template>
          </el-select>
        </el-form-item>
      </el-col> -->
      <el-col :span="6">
        <el-form-item label="业务类型:" prop="quoterBusinessType">
          <el-select v-model="queryParams.quoterBusinessType" clearable>
            <template v-for="dict in dict.type.business_type_cz">
              <el-option :label="dict.label" :value="dict.value" :key="dict.value"></el-option>
            </template>
          </el-select>
        </el-form-item>
      </el-col>
      <el-col :span="6">
        <el-form-item label="月份:" prop="months">
          <el-select v-model="queryParams.months" clearable>
            <template v-for="dict in dict.type.mon_ths">
              <el-option :label="dict.label" :value="dict.value" :key="dict.value"></el-option>
            </template>
          </el-select>
        </el-form-item>
      </el-col>
      <el-col :span="6">
        <el-form-item label="品牌:" prop="carBrandId">
          <el-select v-model="queryParams.carBrandId" filterable style="width: 100%;" @change="getSeriesName">
            <template v-for="dict in brandList">
              <el-option :label="dict.name" :value="dict.id" :key="dict.id"></el-option>
            </template>
          </el-select>
        </el-form-item>
      </el-col>
      <el-col :span="6">
        <el-form-item label="车系:" prop="seriesId">
          <el-select v-model="queryParams.seriesId" filterable style="width: 100%;" @change="getModelName">
            <template v-for="dict in seriesList">
              <el-option :label="dict.name" :value="dict.id" :key="dict.id"></el-option>
            </template>
          </el-select>
        </el-form-item>
      </el-col>
      <el-col :span="6">
        <el-form-item label="车型:" prop="modelId">
          <el-select v-model="queryParams.modelId" filterable style="width: 100%;">
            <template v-for="dict in carIdList">
              <el-option :label="dict.name" :value="dict.id" :key="dict.id"></el-option>
            </template>
          </el-select>
        </el-form-item>
      </el-col>
    </SearchContent>

    <div class="card-wrap card-padding">
      <div class="table-title-wrap justify-end">
        <el-button type="primary" size="mini" @click="handleAdd" v-hasPermi="['enterprise:standardMileageSalvageRate:add']">新增</el-button>
        <el-button type="primary" size="mini" @click="bHandleAdd">批量新增</el-button>
      </div>

      <table-page :loading="loading" :dataList="dataList" border :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @setSizeCurrent="getList">
        <!-- 操作插槽 -->
        <template slot="tableColAc">
          <el-table-column label="操作" width="150" align="center" class-name="small-padding fixed-width">
            <template slot-scope="scope">
              <e-button v-hasPermi="['enterprise:standardMileageSalvageRate:edit']" size="mini" type="text" @click="handleUpdate(scope.row)">编辑
              </e-button>
              <el-button v-hasPermi="['enterprise:standardMileageSalvageRate:void']" type="text" @click="voids(scope.row)">作废</el-button>
            </template>
          </el-table-column>
        </template>
        <!-- 普通列插槽 -->
        <template slot="tableCol">
          <el-table-column label="车辆使用性质" align="center" prop="vehicleUseName" />
          <el-table-column label="品牌" align="center" prop="brandName" />
          <el-table-column label="厂商" align="center" prop="makerName" />
          <el-table-column label="类型" align="center" prop="modelType">
            <template slot-scope="scope">
              <span>{{scope.row.modelType =='1'?'品牌':scope.row.modelType =='2'?'车系':'车型'}}</span>
            </template>
          </el-table-column>
          <el-table-column label="车系/车型" align="center" min-width="200px" show-overflow-tooltip>
            <template slot-scope="scope">
              <span>{{scope.row.modelType =='2'?scope.row.seriesName:scope.row.modelName}}</span>
            </template>
          </el-table-column>
          <el-table-column label="业务类型" align="center" prop="quoterBusinessTypeName" />
          <el-table-column label="标准公里（km）" align="center" prop="stdKilometers" />
          <el-table-column label="月份（月）" align="center" prop="months" />
          <el-table-column label="残值率(%)" align="center">
            <template slot-scope="scope">
              <span>{{numberRide(scope.row.residualValueRate,100) }}%</span>
            </template>
          </el-table-column>
        </template>
      </table-page>
    </div>

    <!-- 添加或修改测试单表对话框 -->
    <el-dialog :title="title" :visible.sync="open" append-to-body class="form-dialog" width="50%">
      <div style="width: 50%; margin: 0 auto;">
        <el-form ref="form" :model="form" label-width="150px" :rules="rules">
          <el-form-item label="车辆使用性质:" prop="vehicleUse">
            <el-select v-model="form.vehicleUse" clearable style="width: 100%;">
              <template v-for="dict in dict.type.vehicle_use">
                <el-option :label="dict.label" :value="dict.value" :key="dict.value"></el-option>
              </template>
            </el-select>
          </el-form-item>
          <el-form-item label="年行驶公里数(km):" prop="stdKilometers">
            <el-select v-model="form.stdKilometers" clearable style="width: 100%;">
              <template v-for="dict in dict.type.kil_ome_tres">
                <el-option :label="dict.label" :value="dict.value" :key="dict.value"></el-option>
              </template>
            </el-select>
          </el-form-item>
          <el-form-item label="业务类型:" prop="quoterBusinessType">
            <el-select v-model="form.quoterBusinessType" clearable style="width: 100%;">
              <template v-for="dict in dict.type.business_type_cz">
                <el-option :label="dict.label" :value="dict.value" :key="dict.value"></el-option>
              </template>
            </el-select>
          </el-form-item>
          <el-form-item label="月份(月):" prop="months">
            <el-select v-model="form.months" clearable style="width: 100%;">
              <template v-for="dict in dict.type.mon_ths">
                <el-option :label="dict.label" :value="dict.value" :key="dict.value"></el-option>
              </template>
            </el-select>
          </el-form-item>
          <el-form-item label="类型:" prop="modelType">
            <el-radio-group v-model="form.modelType" @change="changeType">
              <el-radio :label="2">车系</el-radio>
              <el-radio :label="3">车型</el-radio>
            </el-radio-group>
          </el-form-item>
          <!-- v-if="form.type == 2" -->
          <el-form-item label="车系:" prop="seriesId" v-if="form.modelType == 2">
            <el-select v-model="form.seriesId" filterable clearable style="width: 100%;" placeholder="请输入车系进行搜索选择">
              <template v-for="dict in cityList">
                <el-option :label="dict.seriesName" :value="dict.id" :key="dict.id"></el-option>
              </template>
            </el-select>
          </el-form-item>
          <el-form-item label="车型:" v-if="form.modelType == 3" prop="modelName">
            <el-input style="width: 100%;" v-model="form.modelName" readonly @focus="digCarVehicleType"></el-input>
          </el-form-item>
          <el-form-item label="残值率(%):" prop="residualValueRate">
            <el-input placeholder="请输入残值率" maxlength="10" style="width: 100%;" v-model="form.residualValueRate" @input="form.residualValueRate=form.residualValueRate.replace(/^\D*([0-9]\d*\.?\d{0,5})?.*$/,'$1')" clearable></el-input>
          </el-form-item>
        </el-form>
      </div>
      <div slot="footer" class="dialog-footer" style="text-align: center;">
        <el-button @click="cancel">取消</el-button>
        <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 认</el-button>
      </div>
    </el-dialog>

    <!-- 选择车型弹窗 -->
    <el-dialog title="选择车型" :visible.sync="openDig" append-to-body class="form-dialog" width="55%">
      <SearchContent :queryParams="queryParamsDig" @query="handleQueryDig" @reset="resetQueryDig">
        <el-form :model="queryParamsDig" label-width="50px">
          <el-col :span="5">
            <el-form-item label="品牌:">
              <el-select v-model="queryParamsDig.carBrandId" filterable style="width: 100%;" @change="getSeriesNameDig">
                <template v-for="dict in brandListDig">
                  <el-option :label="dict.name" :value="dict.id" :key="dict.id"></el-option>
                </template>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="车系:">
              <el-select v-model="queryParamsDig.carSeriesId" filterable style="width: 100%;" @change="getModelNameDig">
                <template v-for="dict in seriesListDig">
                  <el-option :label="dict.name" :value="dict.id" :key="dict.id"></el-option>
                </template>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="7">
            <el-form-item label="车型:">
              <el-select v-model="queryParamsDig.Id" filterable style="width: 100%;">
                <template v-for="dict in carIdListDig">
                  <el-option :label="dict.name" :value="dict.id" :key="dict.id"></el-option>
                </template>
              </el-select>
            </el-form-item>
          </el-col>
        </el-form>
      </SearchContent>
      <el-row>
        <table-page :maxHeight="300" :loading="loadingDig" :dataList="dataListDig" border :total="totalDig" :page.sync="queryParamsDig.pageNum" :limit.sync="queryParamsDig.pageSize" @setSizeCurrent="getListDig">
          <!-- 普通列插槽 -->
          <template slot="tableCol">
            <el-table-column label="" align="center" width="35">
              <template scope="scope">
                <el-radio :label="scope.row.id" v-model="radio" @change.native="getCurrentRow(scope.row)">
                  &nbsp;
                </el-radio>
              </template>
            </el-table-column>
            <el-table-column label="品牌" align="center" prop="brandName" />
            <el-table-column label="车系" align="center" prop="seriesName" />
            <el-table-column label="车型" align="center" prop="modelName" />
          </template>
        </table-page>
      </el-row>
      <div slot="footer" class="dialog-footer" style="text-align: center;">
        <el-button @click="closeDig">取 消</el-button>
        <el-button type="primary" @click="submitDig">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

  <script>
import SearchContent from "@/components/SearchContent/index.vue";
import {
  getSalvageList,
  salvageAdd,
  salvageUpdate,
  salvageDelete,
  salvageGet,
  STANDARDMILEAGE,
  TEMPLATEDRIVER,
  getListDig
} from "@/api/globalField";
import { getCarSeriesList } from "@/api/zcwh/vehicleBrandApi.js";
import {
  getBrandNames,
  getSeriesName,
  getModelName,
} from "@/api/system/linkageSelect.js";
export default {
  name: 'Enterprise',
  components: {
    SearchContent,
  },
  dicts: [
    "business_scope",
    "kil_ome_tres",
    "business_type_cz",
    "mon_ths",
    "vehicle_use",
  ],
  data() {
    return {
      queryParamsDig: {
        pageNum: 1,
        pageSize: 10,
      },
      radio: "",
      totalDig: 0,
      brandList: [],
      seriesList: [],
      carIdList: [],

      brandListDig: [],
      seriesListDig: [],
      carIdListDig: [],
      rules: {
        vehicleUse: [
          { required: true, message: "请选择车辆使用性质", trigger: "change" },
        ],
        quoterBusinessType: [
          { required: true, message: "请选择业务类型", trigger: "change" },
        ],
        stdKilometers: [
          { required: true, message: "请选择公里数", trigger: "change" },
        ],
        months: [{ required: true, message: "请选择月份", trigger: "change" }],
        seriesId: [
          { required: true, message: "请选择", trigger: "change" },
        ],
        residualValueRate: [
          { required: true, message: "请输入残值率", trigger: "change" },
        ],
        modelType: [
          { required: true, message: "请选择类型", trigger: "change" },
        ],
        modelName: [
          { required: true, message: "请选择", trigger: "change" },
        ],
      },
      cityList: [],
      //按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      isNew: true,
      // 弹出层标题
      title: "",
      dataListDig: [],
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      },
      total: 0,
      dataList: [],
      // 表单参数
      form: {},
      openDig: false,
      loadingDig: false,
    };
  },
  created() {
    this.getList();
    this.getCarList();
    this.getBrandNames();
  },
  methods: {
    getCurrentRow(row) {
      // 获取选中数据 row表示选中这一行的数据，可以从里面提取所需要的值
      this.modelName = row.modelName;
    },
    handleQueryDig() {
      this.getListDig();
    },
    resetQueryDig() {
      this.queryParamsDig = {
        pageNum: 1,
        pageSize: 10,
      };
      this.seriesListDig = []
      this.carIdListDig = []
      this.getListDig();
    },
    closeDig() {
      this.openDig = false;
    },
    submitDig() {
      this.openDig = false;
      this.$set(this.form, 'modelName', this.modelName)
    },
    // 车型弹窗
    digCarVehicleType() {
      this.openDig = true;
      this.queryParamsDig = {
        pageNum: 1,
        pageSize: 10,
      };
      this.getListDig();
    },
    getListDig() {
      this.loadingDig = true;
      getListDig(this.queryParamsDig).then((res) => {
        if (res.code == "200") {
          const { rows, total } = res;
          this.dataListDig = rows;
          this.totalDig = total;
          this.loadingDig = false;
        }
      });
    },
    changeType() {
      this.$set(this.form, 'seriesId', undefined)
      this.$set(this.form, 'modelName', '')
      this.radio = ''
    },
    // 获取品牌
    getBrandNames() {
      getBrandNames({ brandName: "" }).then((res) => {
        this.brandList = res;
        this.brandListDig = res;
      });
    },
    // 获取车系
    getSeriesName() {
      getSeriesName(this.queryParams?.carBrandId).then((res) => {
        this.seriesList = res;
        this.$set(this.queryParams, 'seriesId', '')
        this.$set(this.queryParams, 'modelId', '')
      });
    },
    // 获取车型
    getModelName() {
      getModelName(this.queryParams.seriesId).then((res) => {
        this.carIdList = res;
        this.$set(this.queryParams, 'modelId', '')
      });
    },

    // 获取车系
    getSeriesNameDig() {
      getSeriesName(this.queryParamsDig.carBrandId).then((res) => {
        this.seriesListDig = res;
        this.$set(this.queryParamsDig, 'carSeriesId', '')
        this.$set(this.queryParamsDig, 'Id', '')
      });
    },
    // 获取车型
    getModelNameDig() {
      getModelName(this.queryParamsDig.carSeriesId).then((res) => {
        this.carIdListDig = res;
        this.$set(this.queryParamsDig, 'Id', '')

      });
    },
    getCarList() {
      getCarSeriesList().then((res) => {
        if (res.code == "200") {
          const { rows, total } = res;
          console.log(rows, total);
          this.cityList = rows;
        }
      });
    },
    voids(row) {
      this.$confirm("确认作废?", "作废", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          salvageDelete(row.id).then((res) => {
            this.getList();
            this.$message({
              type: "success",
              message: "作废成功",
            });
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消作废",
          });
        });
    },
    /** 查询测试单表列表 */
    getList() {
      this.loading = true;
      getSalvageList(this.queryParams).then((res) => {
        if (res.code == "200") {
          const { rows, total } = res;
          this.dataList = rows;
          this.total = total;
          this.loading = false;
        }
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
    },
    reset() {
      this.form = {};
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },

    /** 重置按钮操作 */
    resetQuery() {
      (this.queryParams = {
        pageNum: 1,
        pageSize: 10,
      }),
        this.seriesList = []
      this.carIdList = []
      this.handleQuery();
    },
    /** 新增操作 */
    handleAdd() {
      this.open = true;
      this.title = "新增";
      this.$refs["form"] && this.$refs["form"].resetFields();
      this.reset();
      this.$set(this.form, 'type', null)
      this.$set(this.form, 'modelName', '')
      this.radio = "";
    },
    /** 批量新增操作 */
    bHandleAdd() {
      this.$UploadDialog({
        title: "标准里程残值率",
        showtoastSureBtn: false,
        showResult: false,
        uploadConfig: {
          uploadFileUrl: `#${STANDARDMILEAGE}`,
          limit: 10,
          params: {},
          fileType: ['xls', 'xlsx'],
          templateUrl: `${TEMPLATEDRIVER('carSeriesResidualValueRate')}`,
          templateName: `" 标准里程残值率-导入模版.xlsx`,
        },
      }).then((res) => {
        this.handleQuery()
      }).catch(err => {
        this.handleQuery()
      })
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      console.log(row);
      this.open = true;
      this.title = "编辑";
      salvageGet(row.id).then((res) => {
        this.form = res.data
        this.form.residualValueRate = this.numberRide(this.form.residualValueRate, 100)
        this.form.seriesId = Number(res.data.seriesId)
        this.form.modelId = Number(res.data.modelId)
        // this.modelNameDig = res.data.modelName;
        // this.radio = res.data.modelId;
      });
    },

    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          let form = this._.cloneDeep(this.form)
          form.residualValueRate = this.numberExcept(this.form.residualValueRate, 100)
          form.modelId = this.radio || this.form.modelId
          console.log(this.form, 'this.form');
          if (this.title == "新增") {
            salvageAdd(form).then((res) => {
              this.open = false;
              this.getList();
              this.$message.success("新增成功");
            });
          } else {
            // 调取接口
            salvageUpdate(form).then((res) => {
              this.open = false;
              this.getList();
              this.$message.success("编辑成功");
            });
          }
        }
      });
    },
  },
};
</script>


  <style lang="scss" scoped>
.pagination-container {
  padding: 0 20px 50px !important;
}

.form-dialog {
  ::v-deep .el-dialog__header {
    padding: 10px 20px;
    border-bottom: 1px solid #dcdfe6;

    .el-dialog__headerbtn {
      top: 15px;
    }
  }

  .box-card {
    margin-bottom: 20px;

    .card-title {
      font-size: 16px;
      position: relative;
      margin-left: 8px;
      padding: 0;
    }

    .card-title::after {
      content: "";
      width: 4px;
      height: 18px;
      background-color: aqua;
      display: block;
      position: absolute;
      left: -12px;
      top: 2px;
    }
  }
}
</style>
